<?php
require_once dirname(__FILE__) . '/../common/common.inc.php';
require_once dirname(__FILE__) . "/../database/$database/database.inc.php";

header('Content-Type: text/plain');

global $myManager;

$action = MiscUtils::getParam('action', NULL);

$myManager->start_t();
try {
  switch ($action) {
    case 'login':
      login($myManager);
      break;
    case 'addmodel':
      addmodel($myManager);
      break;
    case 'addtask':
      addtask($myManager);
      break;
    case 'pz_user':
      pz_user($myManager);
      break;
    case 'smry':
      smry($myManager);
      break;
    default:
      break;
  }
} catch (PDOException $e) {
  echo MiscUtils::encode($e->getMessage());
}
$myManager->commit_t();

function login($myManager) {
  $args = json_decode(MiscUtils::getParam('a', '[]'));
  
  $orm = classToOrm('pz_user');
  $result = $orm->find($myManager, NULL, NULL, NULL, NULL, "d.account = '$args->account' AND d.password = '$args->password'", NULL);
  if (count($result->data) == 1) {
    $user = $result->data[0];
    
    $orm_model = classToOrm('pz_model');
    $_SESSION[MODEL] = $orm_model->find($myManager, NULL, NULL, NULL, NULL, NULL, NULL)->data;
    $orm_state = classToOrm('pz_model_state');
    $_SESSION[STATE] = $orm_state->find($myManager, NULL, NULL, NULL, NULL, NULL, NULL)->data;
    $orm_transition = classToOrm('pz_model_transition');
    $_SESSION[TRANSITION] = $orm_transition->find($myManager, NULL, NULL, NULL, NULL, NULL, NULL)->data;
    
    $_SESSION[USER] = $user;
    
    setcookie(MODEL, json_encode($_SESSION[MODEL]), (time() + 3600), PATH, DOMAIN);
    setcookie(STATE, json_encode($_SESSION[STATE]), (time() + 3600), PATH, DOMAIN);
    setcookie(TRANSITION, json_encode($_SESSION[TRANSITION]), (time() + 3600), PATH, DOMAIN);
    setcookie(USER, json_encode($_SESSION[USER]), (time() + 3600), PATH, DOMAIN);
    
    echo json_encode($user);
  } else {
    echo 'INCORRECT ACCOUNT OR PASSWORD';
  }
}

function addmodel($myManager) {
  $args = json_decode(MiscUtils::getParam('a', '[]'));
  
  $orm = classToOrm('pz_model');
  $epModel = $orm->add($myManager, $args->model);
  $myManager->commit_t();
  $myManager->start_t();
  $model = $orm->__toObject($myManager, $epModel, new stdClass());
  echo json_encode($model);
}

function addtask($myManager) {
  $args = json_decode(MiscUtils::getParam('a', '[]'));
  
  $orm = classToOrm('pz_task');
  $epTask = $orm->add($myManager, $args->task);
  $myManager->commit_t();
  $myManager->start_t();
  $task = $orm->__toObject($myManager, $epTask, new stdClass());
  echo json_encode($task);
}

function pz_user($myManager) {
  $args = json_decode(MiscUtils::getParam('a', '[]'));
  
  $orm = classToOrm('pz_user');
  $epUser = $orm->update($myManager, $args->user);
  $myManager->commit_t();
  $myManager->start_t();
  $user = $orm->__toObject($myManager, $epUser, new stdClass());
  
  $_SESSION[USER] = $user;
  setcookie(USER, json_encode($_SESSION[USER]), (time() + 3600), PATH, DOMAIN);
  echo json_encode($user);
}

function smry($myManager) {
  $args = json_decode(MiscUtils::getParam('a', '[]'));

  $smry = array();
  $orm = classToOrm('pz_task');
  foreach ($args->smry as &$arg) {
    try {
      $smry[$arg->name] = array();
      $result = $orm->find($myManager, NULL, NULL, NULL, NULL, $arg->cond, 'COUNT(d.oid)');
      $smry[$arg->name]['total'] = $result->data;
      $result = $orm->find($myManager, NULL, NULL, NULL, NULL, $arg->cond . ' AND d.deadline < ' . time() . ' AND d.is_completed = 0', 'COUNT(d.oid)');
      $smry[$arg->name]['urgent'] = $result->data;
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
  echo json_encode($smry);
}
?>